<?php

namespace app\console\model;

use think\facade\Request;
use think\Loader;
use think\facade\Db;
use think\Exception;
use app\console\model\RolePri;
use think\model\concern\SoftDelete;

class Role extends Base
{
    // 软删除
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = '0000-00-00 00:00:00';


        public static function onAfterWrite($data){
            try {
                Db::startTrans();
                $pri_id = Request::param();
                foreach ($pri_id['pri_id'] as $v) {
                    $array[] = ['role_id' => $data->id, 'pri_id' => $v];
                }
                $action = Request::action();
                if($action == 'update'){
                    Db::name('role_pri')->where(['role_id' => $data->id])->delete();
                }
                (new RolePri)->saveAll($array);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();


            }
        }
    public static function onBeforeDelete($data) {
           Db::name('role_pri')->where('role_id',$data->id)->delete();
        }



}